﻿using ProjScan.RecordManager.AssetData;
using ProjScan.Tools;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;

namespace ProjScan.RecordManager
{
    internal class ReferencedRecordManager : RecordManagerBase<ReferencedAssetData>
    {
        public override string TableTag => "unusedAssets";

        public override void PrepareAssets()
        {
            int num = AssetPathManager.Instance.artPathList.Length;
            datas = new List<ReferencedAssetData>();
            Dictionary<string, List<string>> assetPathDictionary = AssetPathManager.Instance.artPathList.ToDictionary((string assetPath) => assetPath, (string assetPath) => new List<string>());
            BuildAssetPathDictionary(num, out assetPathDictionary);
            for (int i = 0; i < num; i++)
            {
                if (Singleton<ScanInfo>.Instance.isEditor)
                {
                    EditorUtility.DisplayProgressBar("Proj Scan [" + TableTag + "]", "Preparing " + TableTag + "(" + datas.Count + ")...", (float)((double)i * 1.0 / (double)num));
                }

                string text = AssetPathManager.Instance.artPathList[i];
                ActiveAllRecord();
                if (!CheckAssetPathRules(text))
                {
                    continue;
                }

                int iD = AssetPathManager.Instance.GetID(Path.GetDirectoryName(text));
                Type mainAssetTypeAtPath = AssetDatabase.GetMainAssetTypeAtPath(text);
                UnityEngine.Object @object = AssetDatabase.LoadAssetAtPath(text, mainAssetTypeAtPath);
                if (@object != null)
                {
                    ReferencedAssetData referencedAssetData = new ReferencedAssetData(iD, @object, assetPathDictionary[text]);
                    referencedAssetData.id = datas.Count;
                    ValidateAsset(referencedAssetData);
                    referencedAssetData.Reset();
                    datas.Add(referencedAssetData);
                    if (datas.Count % 100 == 0)
                    {
                        ToolsHelper.GC();
                    }
                }
            }
        }

        private void BuildAssetPathDictionary(int assetPathCount, out Dictionary<string, List<string>> assetPathDictionary)
        {
            assetPathDictionary = AssetPathManager.Instance.artPathList.ToDictionary((string assetPath) => assetPath, (string assetPath) => new List<string>());
            for (int i = 0; i < assetPathCount; i++)
            {
                string text = AssetPathManager.Instance.artPathList[i];
                string[] dependencies = AssetDatabase.GetDependencies(text, recursive: false);
                foreach (string text2 in dependencies)
                {
                    if (assetPathDictionary.ContainsKey(text2) && text2 != text)
                    {
                        assetPathDictionary[text2].Add(text);
                    }
                }
            }
        }
    }
}